package com.leiduoduo.oms.service;

import com.leiduoduo.oms.dao.BossDao;
import com.leiduoduo.oms.pojo.Boss;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class BossServiceImpl implements BossService {
    @Autowired
    BossDao bossDao;

    @Override
    public boolean addBoss(Boss boss) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        boss.setCreated_at(simpleDateFormat.format(new Date()))
                .setUpdated_at(simpleDateFormat.format(new Date()));
        return bossDao.addBoss(boss);
    }

    @Override
    public boolean delBoss(Boss boss) {
        boss.setUpdated_at(new SimpleDateFormat().format(new Date()));
        return bossDao.delBoss(boss);
    }

    @Override
    public boolean upBoss(Boss boss) {
        boss.setUpdated_at(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return bossDao.upBoss(boss);
    }

    @Override
    public boolean uptoken(Boss boss) {
        boss.setUpdated_at(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return bossDao.uptoken(boss);
    }

    @Override
    public Boss getBossByCode(String code) {
        System.out.println(code);
        return bossDao.getBossByCode(code);
    }

    @Override
    public List<Boss> getBossList() {
        return bossDao.getBossList();
    }

    /**
     * 按月查营业额
     * @return
     */
    @Override
    public Map<String, Object> getInByMonth(Map<String, Object> map) {
        Map<String, Object>[] maps = bossDao.getInByMonth(map);
        Map<String, Object> result = new HashMap<>();
        String[] months = new String[12];
        String[] amonts = new String[12];
        for (int i = 0; i < 12; i++){
            months[i] = (1+i)+"";
        }
        for (int i = 0; i < 12; i++){
            amonts[i] = 0+"";
        }
        for (Map<String, Object> var:maps) {
            amonts[Integer.parseInt(var.get("month").toString())-1] =  var.get("amont").toString();
        }
        result.put("month",months);
        result.put("amounts",amonts);
        return result;
    }

    /**
     * 按年查营业额
     * @return
     */
    @Override
    public Map<String, Object> getInByYear(Map<String, Object> map) {
        Map<String, Object>[] maps = bossDao.getInByYear(map);
        int year = Integer.parseInt(map.get("year").toString());
        Map<String, Object> result = new HashMap<>();
        String[] years = new String[10];
        String[] amonts = new String[10];
        for (int i = 0; i < 10; i++){
            years[i] = (2020-9+i)+"";
        }
        for (int i = 0; i < 10; i++){
            amonts[i] = 0+"";
        }
        for (Map<String, Object> var:maps) {
            amonts[Integer.parseInt(var.get("year").toString())-year+9] =  var.get("amont").toString();
        }
        result.put("years",years);
        result.put("amounts",amonts);
        return result;
    }


    /**
     * 查询菜品top10
     * @return
     */
    @Override
    public List<Map<String, Object>> getDishTop10(Map<String, Object> map) {
        return bossDao.getDishTop10(map);
    }
}
